package com.intellagric.controller.module.sys_role;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.SysMenu;
import com.intellagric.pojo.SysRole;
import com.intellagric.service.module.sys_role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/13
 * @DESC： 角色角色管理接口
 */
@Controller
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色信息
     * @Param SysRole
     * @return ResponseMessage
     */
    @RequestMapping("/role/add")
    @ResponseBody
    public ResponseMessage add(SysRole sysRole) {
        //初始化数据
        sysRole.setId(UUID.randomUUID().toString().replace("-",""));
        sysRole.setCreateDate(new Date());
        sysRole.setUpdateDate(new Date());
        return roleService.addRole(sysRole);
    }


    /**
     * 删除角色信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("/role/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return roleService.deleteRole(id);
    }

    /**
     * 修改信息
     * @Param SysRole
     * @return ResponseMessage
     */
    @RequestMapping("/role/edit")
    @ResponseBody
    public ResponseMessage edit(SysRole sysRole) {
        sysRole.setCreateDate(null);//不修改创建时间
        sysRole.setUpdateDate(new Date());
        return roleService.editRole(sysRole);
    }

    /**
     * 查询角色信息
     * @Param id
     * @return Object
     */
    @RequestMapping("/role/{id}")
    @ResponseBody
    public SysRole get(@PathVariable String id) {
        return roleService.getRoleById(id);
    }

    /**
     * 查询角色信息列表
     * @return LayuiDataGridResult
     */
    @RequestMapping("/role/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return roleService.getRoleList();
    }

    @RequestMapping("/role/page")
    @ResponseBody
    public LayuiDataGridResult query(int page,int row) {
        LayuiDataGridResult RoleList = roleService.getRoleList(page,row);
        return RoleList;
    }

    /**
     * 通过角色查询角色对应的权限信息列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @RequestMapping("/role/menu")
    @ResponseBody
    public LayuiDataGridResult getMenuByRole(String id) {
        return roleService.getPermissionByRole(id);
    }

}
